package com.hanlink.dsp.ui.widget;

import android.content.Context;

import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.edge.pbxdsp.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


//import butterknife.BindView;
//import butterknife.BindViews;
//import butterknife.ButterKnife;
//import butterknife.Setter;
//import butterknife.ViewCollections;

/**
 * Created by zhb on 2017/11/4.
 */

public class SummingCHView extends LinearLayout implements Checkable {

    private TextView summ_1,summ_2,summ_3,summ_4,summ_5,summ_6,summ_7,summ_8;
    //@BindViews({R.id.summ_1, R.id.summ_2, R.id.summ_3, R.id.summ_4, R.id.summ_5, R.id.summ_6, R.id.summ_7, R.id.summ_8})
    List<TextView> mTextSums;
    //@BindView(R.id.sum_ch)
    TextView mTextCh;

    private static int mCheckColor;
    private static int mNotCheckColor;

    private boolean isChecked = false;
    private Set<Integer> mChsets = new HashSet<>();

    public interface OnCheckedChangeListener {
        void onCheckedChanged(SummingCHView summingCHView, boolean isChecked);
    }

    public OnCheckedChangeListener checkedChangeListener;

    public SummingCHView(Context context) {
        super(context);
        init();
    }

    public SummingCHView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SummingCHView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View view = View.inflate(getContext(), R.layout.summing_ch_view, this);
      //  ButterKnife.bind(this, this);
        summ_1 = view.findViewById(R.id.summ_1);
        summ_2 = view.findViewById(R.id.summ_2);
        summ_3 = view.findViewById(R.id.summ_3);
        summ_4 = view.findViewById(R.id.summ_4);
        summ_5 = view.findViewById(R.id.summ_5);
        summ_6 = view.findViewById(R.id.summ_6);
        summ_7 = view.findViewById(R.id.summ_7);
        summ_8 = view.findViewById(R.id.summ_8);
        mTextSums = new ArrayList<>();
        mTextSums.add(summ_1);
        mTextSums.add(summ_2);
        mTextSums.add(summ_3);
        mTextSums.add(summ_4);
        mTextSums.add(summ_5);
        mTextSums.add(summ_6);
        mTextSums.add(summ_7);
        mTextSums.add(summ_8);
        mTextCh = view.findViewById(R.id.sum_ch);



        mCheckColor = getResources().getColor(com.hanlink.res.R.color.common_text_checked);
        mNotCheckColor = getResources().getColor(com.hanlink.res.R.color.text_normal);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isChecked) {
                    toggle();
                }
            }
        });
    }

//	@OnCheckedChanged(R.id.sum_ch)
//	public void onCheckChanged(CompoundButton button, boolean isChecked) {
//		setChecked(isChecked);
//	}

    public void setChValue(String chValue) {
        mTextCh.setText(chValue);
    }

    public void addSum(int sum) {
        if (mChsets.add(sum)) {
            for (TextView text : mTextSums) {
                if (TextUtils.isEmpty(text.getText())) {
                    text.setText(String.valueOf(sum));
                    return;
                }
            }
        }
    }

    public void clearSum() {
        mChsets.clear();
        for (TextView text : mTextSums) {
            text.setText("");
        }
    }

    public void removeSum(int sum) {
        if (mChsets.remove(sum)) {
            for (TextView text : mTextSums) {
                if (!TextUtils.isEmpty(text.getText()) && text.getText().equals(String.valueOf(sum))) {
                    text.setText("");
                    return;
                }
            }
        }
    }

    @Override
    public void setChecked(boolean checked) {
        if (checked == isChecked) return;
        isChecked = checked;
        mTextCh.setBackgroundResource(checked ?  com.hanlink.res.R.color.common_checked : android.R.color.transparent);
       // ViewCollections.run(mTextSums, isChecked ? CHECKED : NOTCHECK);
      //  ViewCollections.run(mTextCh, isChecked ? CHECKED : NOTCHECK);
        mTextCh.setTextColor(isChecked ? mCheckColor : mNotCheckColor);

        if (checkedChangeListener != null) {
            checkedChangeListener.onCheckedChanged(this, isChecked);
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        int color = getCurrentColor(enabled);
        mTextCh.setTextColor(color);
      //  ViewCollections.set(mTextSums, SET_COLOR, color);
    }

    private int getCurrentColor(boolean enabled) {
        return enabled ? isChecked ? getResources().getColor(com.hanlink.res.R.color.summing_ch_checked_color) : getResources().getColor(com.hanlink.res.R.color.white) : getResources().getColor(com.hanlink.res.R.color.summing_ch_disable_color);
    }

    @Override
    public boolean isChecked() {
        return isChecked;
    }

    @Override
    public void toggle() {
        setChecked(!isChecked);
    }

//    static final Action<TextView> CHECKED = new Action<TextView>() {
//        @Override
//        public void apply(TextView view, int index) {
//            view.setTextColor(mCheckColor);
//        }
//    };
//    static final Action<TextView> NOTCHECK = new Action<TextView>() {
//        @Override
//        public void apply(TextView view, int index) {
//            view.setTextColor(mNotCheckColor);
//        }
//    };

    public void setCheckedChangeListener(OnCheckedChangeListener checkedChangeListener) {
        this.checkedChangeListener = checkedChangeListener;
    }

   // static final Setter<TextView, Integer> SET_COLOR = (view, value, index) -> view.setTextColor(value);
}
